﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data;
using VQWEB.common;
namespace VQWEB.administrator.sources
{
    public class ConfigData
    {
        //webmail_enablessl
        Dictionary<string, string> dic_key = new Dictionary<string, string>();
        public ConfigData()
        {
            //string yahoo = "<a href=\"ymsgr:sendim?xuantruongclub\" ><img src=\"http://opi.yahoo.com/online?u=xuantruongclub&t=1\"></a>";
            //string skype = "<script type=\"text/javascript\" src=\"http://download.skype.com/share/skypebuttons/js/skypeCheck.js\"></script><a href=\"skype:xuantruongclub?call\"><img src=\"http://download.skype.com/share/skypebuttons/buttons/call_blue_white_124x52.png\" style=\"border: none;\" width=\"124\" height=\"52\" alt=\"Skype Me™!\" /></a>";
            //Common
            dic_key.Add("title_web", string.Empty);
            dic_key.Add("meta_keyword", string.Empty);
            dic_key.Add("meta_description", string.Empty);
            dic_key.Add("info_company", string.Empty);

            //Webmail
            dic_key.Add("webmail_smtpserver", string.Empty);
            dic_key.Add("webmail_smtport", "25");
            dic_key.Add("webmail_enablessl", "False");
            dic_key.Add("webmail_username", string.Empty);
            dic_key.Add("webmail_password", string.Empty);

            //Setting
            dic_key.Add("support_online", string.Empty);

            //Close Website
            dic_key.Add("close_website", "False");
            dic_key.Add("close_reason", string.Empty);

            //Google&Facebook
            dic_key.Add("embed_google", string.Empty);
            dic_key.Add("embed_facebook", string.Empty);
        }


        private void initConfigs()
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {

                core_config config = null;
                foreach (KeyValuePair<string, string> item in dic_key)
                {
                    if (CheckConfigsByKey(item.Key) == false)
                    {
                        config = new core_config();
                        config.config_key = item.Key;
                        config.config_value = item.Value;
                        context.core_configs.InsertOnSubmit(config);

                    }
                }
                context.SubmitChanges();
            }
        }

        public DataTable ViewConfigs()
        {
            initConfigs();
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_configs
                            select c;
                if (query.Any())
                    return HelperData.LINQToDataTable(query);
                return new DataTable();
            }

        }

        private bool CheckConfigsByKey(string key)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var Obj = context.core_configs.Where(c => c.config_key == key).SingleOrDefault();
                if (Obj != null)
                    return true;
                else
                    return false;
            }
        }

        public void UpdateConfigs(Dictionary<string, string> dic)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_configs
                            where dic.Keys.Contains(c.config_key)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.config_value = dic[item.config_key];
                        context.SubmitChanges();
                    }

                }
              
            }
        }

    }
}